import Vue from 'vue'
import Router from 'vue-router'
import Login from './views/Login.vue'
import Home from './views/Home.vue'
import Welcome from './views/Welcome.vue'
import Users from './views/Users.vue'
import Cat from './views/Cat.vue'
import Params from './views/Params.vue'
import Goods from './views/Goods'
import GoodsAdd from './views/GoodsAdd.vue'
import Reports from './views/Reports.vue'
Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/login', component: Login, name: 'login' },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: Users },
        { path: '/categories', component: Cat },
        { path: '/params', component: Params },
        { path: '/goods', component: Goods },
        { path: '/goodsadd', component: GoodsAdd },
        { path: '/reports', component: Reports }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  const token = sessionStorage.getItem('token')
  if (!token && to.path !== '/login') {
    return next('/login')
  }
  next()
})

export default router
